package gqz.mvvm.view.custom;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import static android.view.animation.Animation.INFINITE;

/**
 * Created by Administrator on 2017/12/11.
 * PathMeasure的基本使用
 * ValueAnimator的基本使用
 */

public class MyPath extends View {

    String TAG = "MyPath---";

    private int width = 500, height = width;
    private Path path, pathDst;
    private Paint paint;
    private PathMeasure pathMeasure;
    private float pathLength;
    private float value;//ValueAnimator计算出来的结果

    public MyPath(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeWidth(10);

        path = new Path();
        path.addCircle(width / 2, height / 2, width / 2 - 10, Path.Direction.CW);
        pathMeasure = new PathMeasure();
        pathMeasure.setPath(path, false);//将path和pathMeasure绑定
        pathLength = pathMeasure.getLength();//获取path的长度
        pathDst = new Path();

        /**
         * ValueAnimator不直接实现动画，需要动过AnimatorUpdateListener来监听变化，将变化的属性值设置给对象，然后重绘以达到动画的效果
         */
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);//ofFloat()创建实例
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
//                Log.v(TAG, "" + animation.getAnimatedValue());
                value = (float) animation.getAnimatedValue();//获取计算出的属性值
                invalidate();
            }
        });
//        valueAnimator.setDuration(1000);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);//无限重复
        valueAnimator.setRepeatMode(ValueAnimator.REVERSE);//反向
        valueAnimator.setInterpolator(new LinearInterpolator());
//        valueAnimator.start();

        ObjectAnimator rotate = ObjectAnimator.ofFloat(this, View.ROTATION, 0, 720);
        rotate.setRepeatCount(INFINITE);
        rotate.setInterpolator(new LinearInterpolator());

        AnimatorSet set = new AnimatorSet();
        set.playTogether(valueAnimator, rotate);
        set.setDuration(2000);
        set.start();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        pathDst.reset();
        // 硬件加速的BUG
//        pathDst.lineTo(0,0);
        float stop = pathLength * value;
        pathMeasure.getSegment(0, stop, pathDst, true);
        canvas.drawPath(pathDst, paint);
    }
}
